/**
  ETFAna project, Anyang Normal University && IMP-CAS
  \class ETFOnlineSource
  \brief to process data stream from daq while it's working, i.e., during the
  experiment
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-09-28
  \date 2021-09-28 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date        <th>Author      <th>Description                    </tr>
  <tr>  <td>2021-09-28   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#ifndef ETFOnlineSource_h
#define ETFOnlineSource_h

#include <unordered_map>
#include "ETFSource.h"
#include "online_shm.h"

using std::unordered_map;

class ETFDaqData;
class ETFUnpacker;
class ETFOnlineCache;

class ETFOnlineSource : public ETFSource{
public:
  ETFOnlineSource(const string &name = "online");
  virtual ~ETFOnlineSource();

  /// add a daq source
  void AddDaqSource(ETFDaqData *q);
  virtual void Online(); ///< initiate the reading threads
  /// \brief buffered events are read from fQueue and analyzed
  virtual bool ReadEvent(unsigned ev = 0) override; ///< true: success
  virtual void Close() override; ///< close the shm and sockets
  /// true if at least one daq srcs is empty (loaded <= nm)
  virtual bool empty(int nm = 0) const;
  virtual void Clear(); ///< clear all the daqs and data

  /// whether to remove the generated rootfile
  void SetClean(bool opt = true){ fIsClean = opt; }
  virtual bool IsClean() const override{ return fIsClean; }
  ETFOnlineCache *Cache() const{ return fCache; }
  virtual int CacheLoaded() const; // return fCache->loaded()

  // ClassDefOverride(ETFOnlineSource, 0); ///< to process online data

protected:
  /// because PXI event is always larger
  int fData[online_shm_pxi::MAX_LEN]; ///< a provisionary buffer from 'daq' to queue
  int fDropped; ///< dropped events due to low ana speed
  bool fIsClean; ///< whether to remove the generated rootfile
  bool fSkipPXI; ///< all pxi skip one event to match up with VME
  bool fSkipVME; ///< all pxi skip one event to match up with VME
  ETFOnlineCache *fCache;
};

#endif
